#define _CRT_SECURE_NO_WARNINGS 1

#include <queue>
using namespace std;

class Solution {
    bool check[51][51] = { 0 };
    int dx[4] = { 0,0,1,-1 };
    int dy[4] = { 1,-1,0,0 };
    int m;
    int n;
    int ret = 0;
    queue<pair<int, int>> q;
public:
    void bfs(vector<vector<int>>& grid, int r, int c)
    {
        q.push({ r,c });
        int count = 1;
        check[r][c] = true;
        while (q.size())
        {
            auto [a, b] = q.front();
            q.pop();
            for (int i = 0; i < 4; ++i)
            {
                int x = a + dx[i], y = b + dy[i];
                if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == 1 && check[x][y] == false)
                {
                    q.push({ x,y });
                    check[x][y] = true;
                    ++count;
                }
            }
        }
        ret = max(ret, count);
    }
    int maxAreaOfIsland(vector<vector<int>>& grid) {
        m = grid.size(); n = grid[0].size();
        for (int i = 0; i < m; ++i)
        {
            for (int j = 0; j < n; ++j)
            {
                if (grid[i][j] == 1 && check[i][j] == false)
                    bfs(grid, i, j);
            }
        }
        return ret;
    }
};